$(function() {
	//页面初始化加载用户信息
	// 页面加载完成后发起一个Ajax请求
/*	$.ajax({
		url : path + "/MemberServlet",
		type : "post",
		data : {
			method : 'findById',
			existUser : 'sessionScop.existUser'
		},
		dataType : "json",
		success : function(datastr) {
			var data = JSON.parse(datastr);
			
			var portrait = data.portrait;
			var phonenumber = data.phoneNumber
			var nickName = data.nickName;
			var id = data.id;
			var sex = data.sex;
			var province = data.province;
			var city = data.city;
			var school = data.school;
			var password = data.password;
			$("input[name='portrait']").val(portrait);
			$("input[name='phoneNumber']").val(phoneNumber);
			$("input[name='nickName']").val(nickName);
			$("input[name='sex']").each(function() {
				var that = $(this);
				if (that.data("sex") == sex) {
					that.attr("checked", "checked");
				}
			});
			$("#s_province").val(province);
			$("#s_city").val(city);
			$("#s_school").val(school);
			$("input[name='oldPassword']").val(nickName);
			
		}
	})
*/
			
	// 页面加载初始化省份列表
	$(".province_option").remove();
	$.ajax({
		url : "ProvinceServlet",
		type : "get",
		dataType : "json",
		data:{"methodName":"selectAllProvince"},
		success : function(data) {
			for (var i = 0; i < data.data.length; i++) {
				$("#s_province").append(
						"<option class='province_option' value='"
								+ data.data[i].id + "'>" + data.data[i].name
								+ "</option>")
			}
		}
	});

	// 省份改变，获取城市列表
	$("#s_province").on(
			"change",
			function() {
				var provinceId = $("#s_province :checked").val();
				$(".city_option").remove();
				$.ajax({
					url : "CityServlet",
					type : "get",
					dataType : "json",
					data:{
						"methodName":"selectByProvinceId",
						provinceId:provinceId
					},
					success : function(data) {
						for (var i = 0; i < data.data.length; i++) {
							$("#s_city").append("<option class='city_option' value='" + data.data[i].id + "'>" + data.data[i].name + "</option>")
						}
					}
				});
			});

	// 城市改变，获取高校列表
	$("#s_city").on(
			"change",
			function() {
				var cityId = $("#s_city :checked").val();
				$(".school_option").remove();
				$.ajax({
					url : "SchoolServlet",
					type : "get",
					dataType : "json",
					data:{
						"methodName":"selectByCityId",
						cityId:cityId
					},
					success : function(data) {
						for (var i = 0; i < data.data.length; i++) {
							$("#s_school").append("<option class='school_option' value='" + data.data[i].id + "'>" + data.data[i].name + "</option>")
						}
					}
				});
			});
	
	//点击修改页面信息
	$("#alterInfo").click(
			function(){
				var phoneNumber = $("#phoneNumber").val();
				var nickName = $("#nickName").val();
				var sex = $("input[name='sex']").val();	
				var province =$("#s_province").val();
				var city = $("#s_city").val();
				var school = $("#s_school").val();
				var portrait = $("#portrait").attr("src");
				$.ajax({
					url : path+"/MemberServlet",
					type : "post",
					data : {
						methodName : "updateUserMessage",
						phoneNumber:phoneNumber,
						nickName:nickName,
				        sex:sex,
				        province:province,
				        city:city,
				        school:school,
				        portrait:portrait,     
					},
					dataType : "json",
					success :function(data) {
						console.log(data);
					}
			});
			})
			

	$("#alterPwsBtn").click(
			function() {
				var oldPassword = $("#oldPassword").val();
				var newPassword = $("#newPassword").val();
				var passwordAgain = $("#passwordAgain").val();
				if (oldPassword == '') {
					$("#oldPassword").next().css("display", "");
					return;
				} else {
					$("#oldPassword").next().css("display", "none");
				}
				if (newPassword == '') {
					$("#newPassword").next().css("display", "");
					return;
				} else {
					$("#newPassword").next().css("display", "none");
				}
				if (passwordAgain == '') {
					$("#passwordAgain").next().css("display", "");
					return;
				} else {
					$("#passwordAgain").next().css("display", "none");
				}
				if (newPassword != passwordAgain) {
					$("#passwordAgain").next().next().css("display", "");
					return;
				} else {
					$("#passwordAgain").next().next().css("display", "none");
				}
				$.ajax({
					url : "/MemberServlet?member.id=" + memberId
							+ "&member.password=" + oldPassword,
					type : "get",
					success : function(data) {
						if ("true" == data.message) {
							$("#oldPassword").next().next().css("display","none");
									$.ajax({
										url : "member_alterPws?memId="
												+ memberId + "&newPws="
												+ newPassword,
										method : 'updateUserPassword',		
										type : "post",
										success : function(data2) {
											if ("true" == data2.message) {
												alert("修改成功！");
												$("#alterPswForm").find(
														"input[type=password]")
														.val("");
											} else {
												alert("修改失败！");
											}
										}
									})
						} else {
							$("#oldPassword").next().next().css("display", "");
						}
					}
				})
				$.ajax({
					url : path+"/MemberServlet",
					type : "post",
					data : {
						methodName : "updateUserPassword",
						newPassword:newPassword,
					},
					dataType : "json",
					success :function(data) {
						console.log(data);
					}
			});
})
var maxFileSize = 2 * 1024 * 1024;
function checkFile() {
	var file = $("#memPortraitFile").get(0).files[0];
	$("#uploadMessage").css("display", "none");
	if (file) {
		if (!file) {
			$("#uploadMessage").css("display", "");
			$("#uploadMessage").html("请选择一张图片。");
			return false;
		}
		if (file.size > maxFileSize) {
			$("#uploadMessage").css("display", "");
			$("#uploadMessage").html("图片不能超过2M");
			return false;
		}
		if (!/.jpg|.png/i.test($("#memPortraitFile").val())) {
			$("#uploadMessage").css("display", "");
			$("#uploadMessage").html("只支持jpg,png格式的图片");
			return false;
		}
		return true;
	}
}
})
